package com.tgy.JUC.FinalLock;

import java.util.concurrent.atomic.AtomicReference;

/***
 * @ClassName: SpinLockDemo
 * @Description: 测试创建自旋锁
 * @Auther: 送你一块二向箔
 * @Date: 16:43 2022/3/16
 * @version : V1.0
 */
public class SpinLockDemo {
    AtomicReference<Thread> threadAtomicReference = new AtomicReference<>();

    public void myLock(){
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName()+"==> mylock");

        //自旋锁
        while(!threadAtomicReference.compareAndSet(null, thread)){

        }

    }
    public void myUnLock(){
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName()+"==> myUnlock");
        threadAtomicReference.compareAndSet(thread, null);
    }
}
